package com.golxen.utils;


import com.mysql.cj.util.LogUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.*;

public class TimeUtils {

	private static final Logger log = LogUtil.COMMON;

	/**
	 * 一周的秒表示
	 */
	public static final long WEEK_SECONDS = 604800;
	/**
	 * 一天的秒表示
	 */
	public static final long DAY_SECONDS = 86400;
	/**
	 * 一天的毫秒表示
	 */
	public static final long DAY_MILLIONS = 86400000;
	/**
	 * 一小时的秒表示
	 */
	public static final long HOUR_SECONDS = 3600;
	/**
	 * 一小时的毫秒表示
	 */
	public static final long HOUR_MILLIONS = 3600000;
	/**
	 * 一分钟的毫秒表示
	 */
	public static final long MINUTE_MILLIONS = 60000;
	/**
	 * 一分钟的秒表示
	 */
	public static final long MINUTE_SECONDS = 60;
	/**
	 * 一秒的毫秒表示
	 */
	public static final long SECOND_MILLIONS = 1000;

	public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YMDHMSS = "yyyy-MM-dd HH:mm:ss SSS";
	public static final String YMD = "yyyy/M/d";
	public static final String YYMMDD = "yyyyMMdd";

	private static final int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };

	public static boolean isSameWeek(int year, int week, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return ((year == cal.get(Calendar.YEAR)) && (week == cal.get(Calendar.WEEK_OF_YEAR)));
	}

	public static boolean isSameWeek(Date time, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.setFirstDayOfWeek(firstDayOfWeek);
		return isSameWeek(cal.get(Calendar.YEAR), cal.get(Calendar.WEEK_OF_YEAR), firstDayOfWeek);
	}

	/**
	 * 取0点时间戳 （ms）
	 */
	public static long getTodayZeroTimeStamp() {
		long nowTime = System.currentTimeMillis();
		return getZeroTimeStamp(nowTime);
	}

	/**
	 * 取0点时间戳 （s）
	 */
	public static int getTodayZeroTime() {
		long nowTime = System.currentTimeMillis();
		return (int) (getZeroTimeStamp(nowTime) / 1000);
	}

	/**
	 * 判断时间是否是本周内
	 * 
	 * @param time 时间
	 */
	public static boolean isThisWeek(Date time) {
		if (time == null) {
			return false;
		}
		Calendar cal_mon = Calendar.getInstance();
		cal_mon.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		Calendar cal_sun = Calendar.getInstance();
		cal_sun.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal_sun.add(Calendar.WEEK_OF_YEAR, 1);

		return betweenStartAndEnd(cal_mon.getTime(), cal_sun.getTime(), time);
	}

	/**
	 * 判断时间是否是上周内
	 * 
	 * @param time 时间
	 */
	public static boolean isLastWeek(Date time) {
		if (time == null) {
			return false;
		}
		Calendar cal_mon = Calendar.getInstance();
		cal_mon.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal_mon.add(Calendar.WEEK_OF_YEAR, -1);

		Calendar cal_sun = Calendar.getInstance();
		cal_sun.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		return betweenStartAndEnd(cal_mon.getTime(), cal_sun.getTime(), time);
	}

	public static Date firstTimeOfWeek(int firstDayOfWeek, Date time) {
		Calendar cal = Calendar.getInstance();
		if (time != null) {
			cal.setTime(time);
		}
		cal.setFirstDayOfWeek(firstDayOfWeek);
		int day = cal.get(Calendar.DAY_OF_WEEK);

		if (day == firstDayOfWeek) {
			day = 0;
		} else if (day < firstDayOfWeek) {
			day += 7 - firstDayOfWeek;
		} else if (day > firstDayOfWeek) {
			day -= firstDayOfWeek;
		}

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		cal.add(Calendar.DAY_OF_MONTH, -day);
		return cal.getTime();
	}

	/**
	 * 以自然时间即0点区别
	 * 
	 * @param date 时间
	 */
	public static boolean isToday(Date date) {
		if (date == null) {
			return false;
		}
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		Date end = cal.getTime();
		cal.add(Calendar.MILLISECOND, -1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date start = cal.getTime();
		return ((date.after(start)) && (date.before(end)));
	}

	public static String date2String(Date theDate) {
		SimpleDateFormat sdf = getSimpleDateFormat(YMDHMS);
		return date2String(theDate, sdf);
	}

	public static String date2String(Date theDate, String datePattern) {
		if (theDate == null) {
			return "";
		}

		DateFormat format = getSimpleDateFormat(datePattern);
		try {
			return format.format(theDate);
		} catch (Exception e) {
			log.error("", e);
		}
		return "";
	}

	public static String date2String(Date theDate, SimpleDateFormat pattern) {
		if (theDate == null) {
			return "";
		}
		try {
			return pattern.format(theDate);
		} catch (Exception e) {
			log.error("", e);
		}
		return "";
	}

	public static Date string2Date(String dateString, SimpleDateFormat pattern) {
		if ((dateString == null) || (dateString.trim().isEmpty())) {
			return null;
		}
		try {
			return pattern.parse(dateString);
		} catch (ParseException e) {
			log.error("", e);
		}
		return null;
	}

	public static long toMillisSecond(long... seconds) {
		long millis = 0L;
		if ((seconds != null) && (seconds.length > 0)) {
			for (long time : seconds) {
				millis += time * 1000L;
			}
		}
		return millis;
	}

	public static int toSecond(long millis) {
		long second = millis / 1000L;
		return (int) second;
	}

	public static Date changeDateTime(Date theDate, int addDays, int hour, int minute, int second) {
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		cal.add(Calendar.DAY_OF_YEAR, addDays);

		if ((hour >= 0) && (hour <= 24)) {
			cal.set(Calendar.HOUR_OF_DAY, hour);
		}
		if ((minute >= 0) && (minute <= 60)) {
			cal.set(Calendar.MINUTE, minute);
		}
		if ((second >= 0) && (second <= 60)) {
			cal.set(Calendar.SECOND, second);
		}
		return cal.getTime();
	}

	public static Date add(Date theDate, int addHours, int addMinutes, int addSecond) {
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);

		cal.add(Calendar.HOUR_OF_DAY, addHours);
		cal.add(Calendar.MINUTE, addMinutes);
		cal.add(Calendar.SECOND, addSecond);

		return cal.getTime();
	}

	public static int dayOfWeek(Date theDate) {
		if (theDate == null) {
			return -1;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 根据日期返回周几 如周三返回 3 周日返回 7
	 * 
	 * @param date 时间
	 */
	public static int getDayOfWeek(Date date) {
		if (isToday(date)) {
			return weekDays[dayOfWeek(date) - 1];
		} else {
			return weekDays[dayOfWeek(date) - 2];
		}
	}

	/**
	 * 本日期的零点时刻
	 * 
	 * @param theDate 时间
	 */
	public static Date getDate0AM(Date theDate) {
		if (theDate == null) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH))
				.getTime();
	}

	/**
	 * 本日期的明天零点时刻
	 * 
	 * @param theDate 时间
	 */
	public static Date getNextDay0AM(Date theDate) {
		if (theDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH))
				.getTime();
	}

	public static Date getThisDay2359PM(Date theDate) {
		if (theDate == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		Date date = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH)).getTime();
		return new Date(date.getTime() - 1000L);
	}

	/**
	 * 获得结束时间是从开始时间的第几天
	 * 
	 * @param startDate 开始
	 * @param endDate   结束
	 */
	public static int calc2DateTDOADays(Date startDate, Date endDate) {
		if ((startDate == null) || (endDate == null)) {
			return 0;
		}
		Date startDate0AM = getDate0AM(startDate);
		Date endDate0AM = getDate0AM(endDate);
		long v1 = startDate0AM.getTime() - endDate0AM.getTime();
		BigDecimal bd1 = new BigDecimal(v1);
		BigDecimal bd2 = new BigDecimal(DAY_MILLIONS);
		return Math.abs((int) bd1.divide(bd2, 0, 0).doubleValue()) + 1;
	}

	public static Date getNextMonday(Date date) {
		if (date == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDate0AM(date));
		cal.set(Calendar.DAY_OF_WEEK, 2);

		Calendar nextMondayCal = Calendar.getInstance();
		nextMondayCal.setTimeInMillis(cal.getTimeInMillis() + 604800000L);
		return nextMondayCal.getTime();
	}

	public static Date add(int addDay, boolean to0AM) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, addDay);
		Date time = calendar.getTime();
		return ((to0AM) ? getDate0AM(time) : time);
	}

	/**
	 * 返回当前时间的秒数，单位：秒
	 */
	public static int getCurrentSecond() {
		return toSecond(System.currentTimeMillis());
	}

	/**
	 * 返回当前毫秒时间，单位：毫秒
	 */
	public static long getCurrentTime() {
		return System.currentTimeMillis();
	}

	public static Date getCurrentDate() {
		return new Date();
	}

	/**
	 * 比较当前的date是否在[start, end]之间
	 * 
	 * @param start 开始
	 * @param end   结束
	 * @param date  时间
	 */
	public static boolean betweenStartAndEnd(Date start, Date end, Date date) {
		long startMin = start.getTime();
		long endMin = end.getTime();
		long currMin = date.getTime();
		return startMin <= currMin && endMin >= currMin;
	}

	/**
	 * 获取当前时间的月份
	 */
	public static int getMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取yyyy/M/d格式日期
	 * 
	 * @param time 秒
	 */
	public static String getPyyyyMdDateString(long time) {
		SimpleDateFormat sdf = getSimpleDateFormat(YMD);
		return sdf.format(time * 1000);
	}

	/**
	 * 获取yyyy/M/d格式日期
	 * 
	 * @param time 秒
	 */
	public static String getPyyyyMMddDateString(long time) {
		SimpleDateFormat sdf = getSimpleDateFormat(YYMMDD);
		return sdf.format(time * 1000);
	}

	/**
	 * 根据当前日期返回 yyyy-MM-dd HH:mm:ss 格式字符串
	 */
	public static String getCurrentDateTimeString() {
		SimpleDateFormat sdf = getSimpleDateFormat(YMDHMS);
		return sdf.format(Calendar.getInstance().getTime());
	}

	/**
	 * 根据日期返回 yyyy-MM-dd HH:mm:ss 格式字符串
	 */
	public static String getCurrentDateTimeString(long currDate) {
		SimpleDateFormat sdf = getSimpleDateFormat(YMDHMS);
		return sdf.format(currDate);
	}

	/**
	 * 根据 yyyy-MM-dd HH:mm:ss 格式字符串返回日期
	 */
	public static Date getString2Date(String dateStr) {
		Date date = null;
		try {
			SimpleDateFormat sdf = getSimpleDateFormat(YMDHMS);
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			log.error("", e);
		}
		return date;
	}

	/**
	 * 根据日期返回 yyyy-MM-dd HH:mm:ss SSS 格式字符串
	 */
	public static String getCurrentDateTimeStringMi() {
		SimpleDateFormat sdf = getSimpleDateFormat(YMDHMSS);
		return sdf.format(Calendar.getInstance().getTime());
	}

	/**
	 * 获得指定时间、指定天数后的 时间
	 * 
	 * @param date 某时间
	 * @param day  天
	 */
	public static Date getDateAfter(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	/**
	 * 获得当天任意时间
	 * 
	 * @param hour   时
	 * @param minute 分
	 * @param second 秒
	 */
	public static Date getDate(int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.SECOND, second);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获得当天任意时间
	 * 
	 * @param hh 12
	 * @return
	 */
	public static final Date getDateByHours(int hh) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, hh);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获得当前小时数 (24小时制)
	 */
	public static int getHours() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.HOUR_OF_DAY);
	}

	//
	public static int getTimeStampHours(long timestamp) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(timestamp);
		int hour = cal1.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 是否是指定整点时间(小时相等分为0)
	 * 
	 * @param hours 时
	 */
	public static final boolean isHours(int hours) {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.HOUR_OF_DAY) == hours && rightNow.get(Calendar.MINUTE) == 0;
	}

	/**
	 * 在某个时间点范围内
	 * 
	 * @param hour      时
	 * @param minMinute 分
	 * @param maxMinute 分
	 */
	public static final boolean betweenTime(int hour, int minMinute, int maxMinute) {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.HOUR_OF_DAY) == hour && rightNow.get(Calendar.MINUTE) >= minMinute
				&& rightNow.get(Calendar.MINUTE) <= maxMinute;
	}

	/**
	 * 是否是整点时间 分钟为0
	 */
	public static boolean isPoint() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.MINUTE) == 0;
	}

	/**
	 * 取上一个指定的时间
	 * 
	 * @param hour 小时
	 */
	public static Date getLastTime(int hour) {
		// 当前时间大于指定时间，则上一个时间为今天的时间，否则为昨天的时间
		if (getHours() >= hour) {
			return getDateByHours(hour);
		} else {
			return TimeUtils.changeDateTime(TimeUtils.getCurrentDate(), -1, hour, 0, 0);
		}
	}

	public static void main(String[] args) throws Exception {
		System.err.println(getCurrentDateTimeString());
		/*
		 * Date date1 = getString2Date("2015-12-24 15:00:00"); Date date2 =
		 * getString2Date("2015-12-24 15:25:00");
		 * System.out.println(calc2DateTDOADays(date1, date2));
		 * 
		 * 
		 * System.out.println(getTodayWeek()); SimpleDateFormat simpleDateFormat = new
		 * SimpleDateFormat("yyyy-MM-dd");
		 * System.out.println(isSameWeek(simpleDateFormat.parse("2020-06-28"),
		 * simpleDateFormat.parse("2020-06-29")));
		 */
//        System.out.println(getZeroTimeStamp(1598605748000L));
//        System.out.println(getZeroTimeStamp(0));
		System.out.println(calc2DateTDOADays(new Date(1616958000000L), new Date(1617007145000L)));
	}

	public static boolean isSameWeek(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return false;
		}

		// 0.先把Date类型的对象转换Calendar类型的对象
		Calendar todayCal = Calendar.getInstance();
		Calendar dateCal = Calendar.getInstance();

		todayCal.setTime(DateUtils.addDays(date1, -1));
		dateCal.setTime(DateUtils.addDays(date2, -1));

		// 1.比较当前日期在年份中的周数是否相同
		return todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 是否处于同一周
	 * 
	 * @param time1 单位秒
	 * @param time2 单位秒
	 */
	public static boolean isSameWeek(long time1, long time2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(time1 * 1000);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(time2 * 1000);

		return isSameWeek(cal1.getTime(), cal2.getTime());
	}

	/**
	 * 获取当天的星期几
	 */
	public static int getTodayWeek(Calendar now) {
		int week = now.get(Calendar.DAY_OF_WEEK);
		if (week == 1) {
			return 7;
		}
		return week - 1;
	}

	/**
	 * 获取当天的星期几
	 */
	public static int getTodayWeek() {
		return getTodayWeek(Calendar.getInstance());
	}

	public static Date getDate(int year, int month, int day, int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(year, month, day, hour, minute, second);
		return cal.getTime();
	}

	// 是否同一个月
	public static boolean isSameMonth(long updateTime) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(System.currentTimeMillis());

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(updateTime);
		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
	}

	/**
	 * 是否是相邻两天一天
	 * 
	 * @param smallTime 单位秒
	 * @param bigTime   单位秒
	 */
	public static boolean isNextDay(long smallTime, long bigTime) {
		return isSameDay(smallTime + DAY_SECONDS, bigTime);
	}

	/**
	 * 是否是同一天
	 * 
	 * @param time1 单位秒
	 * @param time2 单位秒
	 */
	public static boolean isSameDay(long time1, long time2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(time1 * 1000);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(time2 * 1000);
		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
				&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 是否同一个小时内
	 * 
	 * @param time1 单位毫秒
	 * @param time2 单位毫秒
	 */
	public static boolean isSameHour(long time1, long time2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(time1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(time2);
		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
				&& cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)
				&& cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 是否同一个月份
	 * 
	 * @param time1 单位秒
	 * @param time2 单位秒
	 */
	public static boolean isSameMonth(long time1, long time2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(time1 * 1000);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(time2 * 1000);
		return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
				&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
	}

	/**
	 * 获取上周周一0点时间
	 * 
	 * @param date 某时间
	 */
	public static Date geLastWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, -7);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取这周周一0点时间
	 * 
	 * @param date 某时间
	 */
	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取下周周一0点时间
	 * 
	 * @param date 某时间
	 */
	public static Date getNextWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, 7);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	// 定制間隔重置(代码写错了，这个方法永远都不要用了，小于7天重置就会出问题)
	public static Date getNextResetDate(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static boolean isValidQueryDate(String str) {
		boolean convertSuccess = true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
		try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;
	}

	public static long getQueryDateTime(String str) {
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
		try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			Date date = format.parse(str);
			return date.getTime();
		} catch (ParseException e) {
			// e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	public static long str2timestamp(String time) {
		long timeStamp = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = format.parse(time);
			// 日期转时间戳（毫秒）
			timeStamp = date.getTime();
		} catch (Exception e) {

		}
		return timeStamp;
	}

	// 获取时间戳的当日零点时间戳 毫秒
	public static long getZeroTimeStamp(long inTime) {
		long todayStartTime = inTime - (inTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
		return todayStartTime;
	}

	/**
	 * 获取时间戳的当日零点时间戳 s
	 * 
	 * @param inTime s
	 * @return
	 */
	public static int getZeroTime(int inTime) {
		return inTime - (inTime + TimeZone.getDefault().getRawOffset()) % (3600 * 24);
	}

	/**
	 * 获取当前日期与指定日期相差几天
	 *
	 * @param time 指定日期的时间戳(s)
	 * @return
	 */
	public static long getDiffDayNum(long time) {
		long todayZeroTime = getTodayZeroTimeStamp() / 1000;
		if (time >= todayZeroTime) {
			// 注意 有可能为负数
			return (todayZeroTime - time) / DAY_SECONDS;
		}
		return (todayZeroTime - 1 - time) / DAY_SECONDS + 1;
	}

	/**
	 * 根据当前日期返回 dateFormat 格式字符串
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static String getCurrentDateTimeString(String dateFormat) {
		SimpleDateFormat sdf = getSimpleDateFormat(dateFormat);
		return sdf.format(Calendar.getInstance().getTime());
	}

	/**
	 * 获取本周一0点的时间戳 (s)
	 */
	public static int getWeekBeginTimestamp() {
		long secs = getCurrentSecond();
		return getWeekBeginTimestamp(secs);
	}

	/**
	 * 获取当前时间的周一0点的时间戳 (s)
	 * 
	 * @param time 要计算的时间
	 * @return
	 */
	public static int getWeekBeginTimestamp(long time) {
		return (int) (time - ((time + (TimeZone.getDefault().getRawOffset() / 1000)) % DAY_SECONDS)
				- ((getDays1970(time) + 3) % 7) * DAY_SECONDS);// 0星期1...6星期日
	}

	/** 通过当前秒数获取1970年至今的天数 */
	public static int getDays1970(long timeSeconds) {
		return (int) ((timeSeconds + (TimeZone.getDefault().getRawOffset() / 1000)) / DAY_SECONDS);
	}

	/**
	 * 获取当前时间 下周一0点的时间
	 * 
	 * @param time 要计算的时间 (s)
	 * @return
	 */
	public static int getNextMonDayTime(long time) {
		// 当前时间这周一0点的时间
		int curMonDayTime = getWeekBeginTimestamp(time);
		return (int) (curMonDayTime + TimeUtils.WEEK_SECONDS);
	}

	//当天凌晨的时间戳
	public static long getZeroTimeTimeStamp() {
		LocalDateTime nowTime = LocalDateTime.now();
		LocalDateTime zeroDate = LocalDateTime.of(nowTime.getYear(), nowTime.getMonth(), nowTime.getDayOfMonth(), 0, 0,
				0);

		return zeroDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
	}

	/**
	 * 获取某个时间戳 当天凌晨 的时间戳
	 * 
	 * @param timeMillis
	 * @return
	 */
	public static long getZeroTimeTimeStamp(long timeMillis) {
		LocalDateTime time = LocalDateTime.ofEpochSecond(timeMillis / 1000, 0,
				ZoneOffset.ofHours(getDefaultTimeZone()));
		LocalDateTime zeroDate = LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), 0, 0, 0);
		long zeroTime = zeroDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

		return zeroTime;
	}

	public static boolean isSameWeek(LocalDateTime time) {
		LocalDateTime today = LocalDateTime.now();
		return today.get(ChronoField.ALIGNED_WEEK_OF_YEAR) == time.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
	}

	public static int getDefaultTimeZone() {
		return TimeZone.getDefault().getRawOffset() / 3600000;
	}

	private static Logger logger = LogUtil.COMMON;

	/**
	 * 是否是新的一天(凌晨12点)
	 * 
	 * @param date 比较的日期
	 * @return
	 */
	public static boolean isNewDay(Date date) {
		if (date == null) {
			return false;
		}
		Calendar nextDate = getCalendar(date);
		nextDate.add(Calendar.DATE, 1);
		nextDate.set(Calendar.HOUR_OF_DAY, 0);
		nextDate.set(Calendar.MINUTE, 0);
		nextDate.set(Calendar.SECOND, 0);
		nextDate.set(Calendar.MILLISECOND, 0);
		return nextDate.getTimeInMillis() <= System.currentTimeMillis() ? true : false;
	}

	/**
	 * 获取系统距1970年1月1日总毫秒
	 * 
	 * @return
	 */
	// TODO sk(done) 删除这个方法性能太差
	public static long getSysCurTimeMillis() {
		return getCalendar().getTimeInMillis();
	}

	public static int getSysCurDay() {
		Calendar calendar = getCalendar();
		int y = calendar.get(Calendar.YEAR);
		int m = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		return y * 10000 + m * 100 + day;
	}

	/**
	 * 获取系统距1970年1月1日总秒
	 * 
	 * @return
	 */
	public static long getSysCurSeconds() {
		return getCalendar().getTimeInMillis() / 1000;
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static Timestamp getSysteCurTime() {
		Timestamp ts = new Timestamp(getCalendar().getTimeInMillis());
		return ts;
	}

	public static Timestamp getSysMonth() {
		Calendar now = getCalendar();
		now.set(Calendar.DAY_OF_MONTH, 1);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		formatter.format(now.getTime());
		return new Timestamp(now.getTimeInMillis());
	}

	/**
	 * 获取指定日期距1970年1月1日总秒
	 *
	 * @param date
	 * @return
	 */
	public static long getDateToSeconds(Date date) {
		return getCalendar(date).getTimeInMillis() / 1000;
	}

	/**
	 * 获取时间的秒
	 *
	 * @param time
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getTimeToSeconds(Time time) {
		if (time != null)
			return time.getHours() * 3600 + time.getMinutes() * 60 + time.getSeconds();
		return 0;
	}

	@SuppressWarnings("deprecation")
	public static Time getSecondsToTime(int seconds) {
		Time time = new Time(seconds / 3600, seconds % 3600 / 60, seconds % 3600 % 60);
		return time;
	}

	/**
	 * 获取当前时间的秒
	 *
	 * @return
	 */
	public static int getSysTimeSeconds() {
		Calendar cal = getCalendar();
		return cal.get(Calendar.HOUR_OF_DAY) * 3600 + cal.get(Calendar.MINUTE) * 60 + cal.get(Calendar.SECOND);
	}

	/**
	 * 获取指定日期距1970年1月1日总毫秒
	 * 
	 * @param date
	 * @return
	 */
	public static long getDateToMillis(Date date) {
		return getCalendar(date).getTimeInMillis();
	}

	/**
	 * 获取当前小时
	 * 
	 * @return
	 */
	public static int getCurrentHour() {
		return getCalendar().get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获 取当前分钟
	 * 
	 * @return
	 */
	public static int getCurrentMinute() {
		return getCalendar().get(Calendar.MINUTE);
	}

//	/**
//	 * 获取当前分钟
//	 * 
//	 * @return
//	 */
//	public static int getCurrentSecond() {
//		return getCalendar().get(Calendar.SECOND);
//	}

	/**
	 * 获取当前天
	 */
	public static int getCurrentDay() {
		return getCalendar().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取当前天（一年）
	 */
	public static int getCurrenDayOfYear() {
		return getCalendar().get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 指定的毫秒long值转成Timestamp类型
	 * 
	 * @param value
	 * @return
	 */
	public static Timestamp getMillisToDate(long value) {
		return new Timestamp(value);
	}

	/**
	 * 指定的秒long值转成Timestamp类型
	 * 
	 * @param value
	 * @return
	 */
	public static String getStringDate(long value) {
		if (value == 0) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 设置格式
		String timeText = format.format(value * 1000);
		return timeText;
	}

	/**
	 * 当前系统时间增加值
	 *
	 * @param type
	 * @param value
	 * @return
	 */
	public static Date addSystemCurTime(int type, int value) {
		Calendar cal = getCalendar();
		switch (type) {
		case Calendar.DATE:// 增加天数
			cal.add(Calendar.DATE, value);
			break;
		case Calendar.HOUR:// 增加小时
			cal.add(Calendar.HOUR, value);
			break;
		case Calendar.MINUTE:// 增加分钟
			cal.add(Calendar.MINUTE, value);
			break;
		case Calendar.SECOND:// 增加秒
			cal.add(Calendar.SECOND, value);
			break;
		case Calendar.MILLISECOND:// 增加毫秒
			cal.add(Calendar.MILLISECOND, value);
			break;
		default:
			break;
		}
		return new Date(cal.getTimeInMillis());
	}

	public static Date getNextDate() {
		Calendar cal = getCalendar();
		cal.add(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.add(Calendar.MILLISECOND, 0);
		return new Date(cal.getTimeInMillis());
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateFormat(Date date) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ctime = formatter.format(date);
		return ctime;
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String ctime = formatter.format(date);
		return ctime;
	}

	/**
	 * 获取默认日期2000-01-01
	 * 
	 * @return 返回默认起始时间
	 */
	public static Timestamp getDefaultDate() {
		Date defaultDate = null;
		try {
			defaultDate = (Date) new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parseObject("2000-01-01 00:00:00");

		} catch (ParseException e) {
			logger.error("异常错误", e);
		}
		return new Timestamp(defaultDate.getTime());
	}

	/**
	 * 获取默认目上限日期2999-01-01
	 * 
	 * @return 返回默认上限时间
	 */
	public static Timestamp getDefaultMaxDate() {
		Date defaultDate = null;
		try {
			defaultDate = (Date) new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parseObject("2999-01-01 00:00:00");
		} catch (ParseException e) {
			logger.error("异常错误", e);
		}
		return new Timestamp(defaultDate.getTime());
	}

	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static boolean dateCompare(Date date) {
		if (date == null)
			return false;
		Calendar now = getCalendar();
		Calendar other = getCalendar(date);
		return dateCompare(now, other) == 0 ? true : false;
	}

	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static boolean dateCompare(long date) {
		Calendar now = getCalendar();
		Calendar other = getCalendar(getMillisToDate(date));
		return dateCompare(now, other) == 0 ? true : false;
	}

	/**
	 * <pre>
	 * 比较是否为同一天(注意：分界线为凌晨 5 点)
	 * </pre>
	 *
	 * @param date
	 * @return
	 */
	public static boolean dataCompare5(Date date) {
		if (date == null)
			return false;
		Calendar now = getCalendar();
		now.add(Calendar.HOUR_OF_DAY, -5);
		Calendar other = getCalendar(date);
		other.add(Calendar.HOUR_OF_DAY, -5);
		if (dateCompare(now, other) == 0) {
			return true;
		}
		return false;
	}

	/**
	 * <pre>
	 * 比较是否为同一天(注意：分界线为凌晨 3 点)
	 * </pre>
	 *
	 * @param date
	 * @return
	 */
	public static boolean dataCompare3(Date date) {
		if (date == null)
			return false;
		Calendar now = getCalendar();
		now.add(Calendar.HOUR_OF_DAY, -3);
		Calendar other = getCalendar(date);
		other.add(Calendar.HOUR_OF_DAY, -3);
		if (dateCompare(now, other) == 0) {
			return true;
		}
		return false;
	}

	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean dataCompare(Date date1, Date date2) {
		if (date1 == null || date2 == null)
			return false;
		Calendar c1 = getCalendar(date1);
		Calendar c2 = getCalendar(date2);
		return dateCompare(c1, c2) == 0 ? true : false;
	}

	/**
	 * 返回两个日期相差天数
	 *
	 * @param startDate 开始日期
	 * @param endDate   结束日期
	 * @return
	 */
	public static int dateCompare(Calendar startDate, Calendar endDate) {
		startDate.set(Calendar.HOUR_OF_DAY, 0);
		startDate.set(Calendar.MINUTE, 0);
		startDate.set(Calendar.SECOND, 0);
		startDate.set(Calendar.MILLISECOND, 0);

		endDate.set(Calendar.HOUR_OF_DAY, 0);
		endDate.set(Calendar.MINUTE, 0);
		endDate.set(Calendar.SECOND, 0);
		endDate.set(Calendar.MILLISECOND, 0);

		int day = (int) (endDate.getTimeInMillis() / 1000 / 60 / 60 / 24
				- startDate.getTimeInMillis() / 1000 / 60 / 60 / 24);
		return day;
	}

	/**
	 * <pre>
	 * 比较日期是否同一天(注意：分界线为晚上 12 点)
	 * </pre>
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws Exception
	 */
	public static int dateCompare(Date startDate, Date endDate) {
		if (startDate == null || endDate == null) {
			logger.error("日期比较出现异常 数值为 null");
			return 0;
		}
		Calendar c1 = getCalendar(startDate);
		Calendar c2 = getCalendar(endDate);
		return dateCompare(c1, c2);
	}

	/**
	 * <pre>
	 * 返回两个日期相差天数(注意：分界线为凌晨 5 点)
	 * </pre>
	 * 
	 * @return
	 */
	public static int dateCompare5(Date startDate, Date endDate) {
		if (startDate == null || endDate == null) {
			return 0;
		}
		Calendar c1 = getCalendar(startDate);
		c1.add(Calendar.HOUR_OF_DAY, -5);
		Calendar c2 = getCalendar(endDate);
		c2.add(Calendar.HOUR_OF_DAY, -5);
		return dateCompare(c1, c2);
	}

	/**
	 * 比较日期是否是同一个月份
	 *
	 * @param date 被比较的日期
	 * @return
	 */
	public static boolean monthCompare(Date date) {// 一年之内是否是同一个月
		if (date == null)
			return false;
		Calendar now = getCalendar();
		Calendar other = getCalendar(date);
		int nowMonth = now.get(Calendar.MONTH) + 1;
		int otherMonth = other.get(Calendar.MONTH) + 1;
		return (otherMonth - nowMonth) == 0 ? true : false;
	}

	/**
	 * 获取该月的天数
	 *
	 * @return
	 */
	public static int monthDays() {// 返回当前月份的天数
		Calendar now = getCalendar();
		return now.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取当前是该月的第几天
	 *
	 * @return
	 */
	public static int monthDay() {
		Calendar now = getCalendar();
		return now.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 重置防沉迷刷新时间
	 *
	 * @param hour        刷新时间点
	 * @param refreshTime 刷新时间引用
	 */
	public static void setAASRefreshTime(int hour, Calendar refreshTime) {
		refreshTime.setTime(getSysteCurTime());
		refreshTime.set(Calendar.HOUR_OF_DAY, hour);
		refreshTime.set(Calendar.MINUTE, 0);
		refreshTime.set(Calendar.SECOND, 0);
	}

	public static long calcDistanceMillis(Date startTime, Date endTime) {
		long startSecond = getDateToSeconds(startTime);
		long endSecond = getDateToSeconds(endTime);
		return (endSecond - startSecond) * 1000;
	}

	/**
	 * 间隔时间以小时为单位
	 *
	 * @param startDate
	 * @param interval
	 * @return
	 */
	public static boolean isInterval(Date startDate, int interval) {
		return dataCompare5(startDate);
	}

	public static int timeToFrame(int secondTime) {
		return (secondTime * 25) / 1000;
	}

	public static String getSignStr() {
		String[] strs = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
				"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

		StringBuilder signStr = new StringBuilder();
		for (int i = 0; i < 6; i++) {
			int j = RandomUtils.nextInt(0,strs.length);
			signStr.append(strs[j]);

		}
		return signStr.toString();
	}

	/**
	 * 获取系统时间
	 *
	 * @return
	 */
	public synchronized static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	/**
	 * 获取指定的时间
	 *
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		return calendar;
	}

	public static Timestamp getCalendarToDate(Calendar calendar) {
		if (calendar != null)
			return new Timestamp(getCalendar().getTimeInMillis());
		return null;
	}

	public static Date addDate(Date date, long value) {
		long time = date.getTime() + value;
		return new Date(time);
	}

	/**
	 * 把日期类型转换为字节数组
	 *
	 * @param date
	 * @return
	 */
	public static byte[] dateToBytes(Date date) {
		Calendar calendar = Calendar.getInstance();
		byte[] byteArray = new byte[7];
		calendar.setTime(date);
		short year = (short) calendar.get(Calendar.YEAR);
		byteArray[0] = (byte) ((year >>> 8) & 0xFF);
		byteArray[1] = (byte) (year & 0xFF);
		byteArray[2] = (byte) (calendar.get(Calendar.MONTH) + 1);
		byteArray[3] = (byte) calendar.get(Calendar.DATE);
		byteArray[4] = (byte) calendar.get(Calendar.HOUR_OF_DAY);
		byteArray[5] = (byte) calendar.get(Calendar.MINUTE);
		byteArray[6] = (byte) calendar.get(Calendar.SECOND);
		return byteArray;
	}

	public static Date getSunday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		Date monday = currentDate.getTime();
		return monday;
	}

	public static Date getNextMonday() {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		Date monday = currentDate.getTime();
		return monday;
	}

	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期一是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}

	public static int getDayOfWeekIndex() {
		Calendar calendar = Calendar.getInstance();
		int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (index == 0) {
			index = 7;
		}
		return index;
	}

	public static boolean isTimeOut(Date expDate) {
		Calendar curentDate = Calendar.getInstance();
		Calendar expirtDate = Calendar.getInstance();
		expirtDate.setTime(expDate);

		long intervalMillis = expirtDate.getTimeInMillis() - curentDate.getTimeInMillis();
		return intervalMillis <= 0;
	}

	public static Date getSaturday(int nextWeek) {
		int mondayPlus = getMondayPlus();
		if (nextWeek > 0) {
			mondayPlus = mondayPlus + (nextWeek * 7);
		}
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 5);
		currentDate.set(Calendar.HOUR_OF_DAY, 5);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		Date saturday = currentDate.getTime();
		return saturday;
	}

	public static boolean isSaturday() {
		int dayIndex = getDayOfWeekIndex();
		if (6 == dayIndex) {
			return true;
		}
		return false;
	}

	public static Date parseDate(String dateStr) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = df.parse(dateStr);
			return date;
		} catch (ParseException e) {
			logger.error("异常错误", e);
		}
		return null;
	}

	public static boolean isInDate(long curTimeMillis, Date openDate, Date stopDate) {
		if (openDate == null || stopDate == null) {
			return false;
		}
		long openMillis = getDateToMillis(openDate);
		long stopMillis = getDateToMillis(stopDate);
		return isInDate(curTimeMillis, openMillis, stopMillis);
	}

	public static boolean isInDate(long curTimeMillis, long openMillis, long stopMillis) {
		return curTimeMillis >= openMillis && curTimeMillis <= stopMillis;
	}

	public static boolean isAfter(long currentMillis, Date date) {
		if (date == null || date == null) {
			return false;
		}
		long openMillis = getDateToMillis(date);
		return currentMillis >= openMillis;
	}

	public static Date addTime(Date current, int type, int value) {
		Calendar cal = getCalendar(current);
		switch (type) {
		case Calendar.DATE:// 增加天数
			cal.add(Calendar.DATE, value);
			break;
		case Calendar.HOUR:// 增加小时
			cal.add(Calendar.HOUR, value);
			break;
		case Calendar.MINUTE:// 增加分钟
			cal.add(Calendar.MINUTE, value);
			break;
		case Calendar.SECOND:// 增加秒
			cal.add(Calendar.SECOND, value);
			break;
		case Calendar.MILLISECOND:// 增加毫秒
			cal.add(Calendar.MILLISECOND, value);
			break;
		default:
			break;
		}
		return new Date(cal.getTimeInMillis());
	}

	public static boolean isSaturday(Date date) {
		int dayIndex = getDayOfWeekIndex(date);
		if (6 == dayIndex) {
			return true;
		}
		return false;
	}

	public static int getDayOfWeekIndex(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (index == 0) {
			index = 7;
		}
		return index;
	}

	/** 上周一 00:00:01，譬如：当前时间 2013-05-03 返回：2013-04-29 00:00:30 */
	public static Date getSunday(int nextWeek) {
		int mondayPlus = getMondayPlus();
		if (nextWeek > 0) {
			mondayPlus = mondayPlus + (nextWeek * 7);
		}
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 30);
		Date saturday = currentDate.getTime();
		return saturday;
	}

	/**
	 * <pre>
	 * 获取 date 所在周的周日日期
	 * </pre>
	 * 
	 * @param date
	 * @return
	 */
	public static Date getCurrentWeekEndDate(Date date) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(date);

		// 取当前日期是星期几(week:星期几)
		int week = aCalendar.get(Calendar.DAY_OF_WEEK) - 1;
		int count = 0;
		if (week == 1) {
			count = 6;
		} else if (week == 2) {
			count = 5;
		} else if (week == 3) {
			count = 4;
		} else if (week == 4) {
			count = 3;
		} else if (week == 5) {
			count = 2;
		} else if (week == 6) {
			count = 1;
		}

		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_WEEK, count);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	public static Calendar getNextDateByIndex(int indexOfWeek) {
		int addDay = 0;
		int mondayPlus = getDayOfWeekIndex();
		if (mondayPlus == 7) {
			addDay = 0;
		} else if (mondayPlus == 6) {
			addDay = 1;
		} else if (mondayPlus == 5) {
			addDay = 2;
		} else if (mondayPlus == 4) {
			addDay = 3;
		} else if (mondayPlus == 3) {
			addDay = 4;
		} else if (mondayPlus == 2) {
			addDay = 5;
		} else if (mondayPlus == 1) {
			addDay = 6;
		}
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, addDay + indexOfWeek);
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		return currentDate;
	}

	/**
	 * <pre>
	 * 获取两日期毫秒时间差
	 * （date2 > date1）否则返回0
	 * </pre>
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getMillisBetweenDates(Date date1, Date date2) {
		if (date1.before(date2)) {
			return date2.getTime() - date1.getTime();
		}
		return 0;
	}

	/**
	 * <pre>
	 * 获得上周一的凌晨几点、几分
	 * </pre>
	 * 
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static Date getLastMonday(int hour, int minute) {
		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		currentDate.set(Calendar.HOUR_OF_DAY, hour);
		currentDate.set(Calendar.MINUTE, minute);
		currentDate.set(Calendar.SECOND, 0);
		Date monday = currentDate.getTime();
		return monday;
	}

	private static final Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

	public static final SimpleDateFormat getSimpleDateFormat(final String pattern) {
		ThreadLocal<SimpleDateFormat> sdf = sdfMap.get(pattern);
		if (sdf == null) {
			sdf = new ThreadLocal<SimpleDateFormat>() {
				@Override
				protected SimpleDateFormat initialValue() {
					return new SimpleDateFormat(pattern);
				}
			};
			sdfMap.put(pattern, sdf);
		}
		return sdf.get();
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateFormat(Date date, String format) {
		SimpleDateFormat dateFm = getSimpleDateFormat(format);
		if (date == null)
			date = getDefaultDate();
		return dateFm.format(date);
	}

}
